package com.winona;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.taplinker.client.R;
import com.taplinker.client.im.activity.IMQueueActivity;
import com.taplinker.client.im.domain.ImDataCenter;
import com.winona.ui.AcademicCenterActivity;
import com.winona.ui.PerToolActivity;
import com.winona.ui.PersonalSettingActivity;

public class HomeActivity extends Base1Activity {
	private SharedPreferences sp;
	private GridView gv_home;
	private ImageView back;
	private Intent intent;
	private TextView title;
	private Button bt_login;
	private TextView mNavPrivateCount;

	private String[] names = { "我的会话", "学术中心", "个人工具", "个人设置" };

	private int[] icons = { R.drawable.home_conversation,
			R.drawable.home_acmic_center, R.drawable.home_center,
			R.drawable.home_set };

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		sp = getSharedPreferences("config", MODE_PRIVATE);
		setContentView(R.layout.win_home);
		findView();
		title.setText("皮肤科在线");

		gv_home.setAdapter(new HomeAdapter());
		setListener();

	}

	private void findView() {
		gv_home = (GridView) findViewById(R.id.gv_home);
		title = (TextView) findViewById(R.id.tv_title);
		mNavPrivateCount = (TextView) findViewById(R.id.nav_private_counts);

	}

	private void setListener() {
		gv_home.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				switch (position) {
				case 0:
					intent = new Intent(HomeActivity.this,
							IMQueueActivity.class);
					intent.putExtra("conversation", "1");
					startActivity(intent);

					break;
				case 1:
					intent = new Intent(HomeActivity.this,
							AcademicCenterActivity.class);
					intent.putExtra("academic", "1");
					startActivity(intent);
					overridePendingTransition(R.anim.input, R.anim.out);
					break;
				case 2:
					intent = new Intent(HomeActivity.this,
							PerToolActivity.class);
					intent.putExtra("tool", "1");
					startActivity(intent);
					overridePendingTransition(R.anim.input, R.anim.out);
					break;
				case 3:
					intent = new Intent(HomeActivity.this,
							PersonalSettingActivity.class);
					intent.putExtra("set", "1");
					startActivity(intent);
					overridePendingTransition(R.anim.input, R.anim.out);
					break;

				default:
					break;
				}

			}
		});

	}

	// 处理返回键
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			exitDialog();
			return false;
		}
		return super.onKeyDown(keyCode, event);
	}

	// 退出程序
	public void exitDialog() {
		new AlertDialog.Builder(this).setMessage("是否退出程序？")
				.setPositiveButton("确定", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
						setResult(RESULT_OK);
						HomeActivity.this.finish();
						System.exit(0);
					}
				})
				.setNegativeButton("取消", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
						return;
					}
				}).create().show();
	}

	private class HomeAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return names.length;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View view = View.inflate(getApplicationContext(),
					R.layout.grid_home_item, null);
			ImageView iv_logo = (ImageView) view
					.findViewById(R.id.iv_item_logo);
			TextView tv_name = (TextView) view.findViewById(R.id.tv_item_name);
			tv_name.setText(names[position]);
			iv_logo.setImageResource(icons[position]);
			if (position == 0) {// 第0个条目{
				String newname = sp.getString("newname", "");
				if (!TextUtils.isEmpty(newname)) {
					tv_name.setText(newname);
				}
			}
			return view;
		}

		@Override
		public Object getItem(int position) {
			return null;
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

	}

	@Override
	public void handleMessage(String type) {
		if (type.equals(ImDataCenter.HOME_REFRESH)) {

			if (ImDataCenter.getInstance().getPriNum() > 0) {
				mNavPrivateCount.setText(String.valueOf(ImDataCenter
						.getInstance().getPriNum()));
				mNavPrivateCount.setVisibility(View.VISIBLE);
			} else {
				mNavPrivateCount.setVisibility(View.GONE);
			}

		}

	}

}
